package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.model.bean.CommodityEvaluateBean;
import com.afkt.project.ui.adapter.MultiSelectAdapter;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import com.dev.DevUtils;
import com.dev.base.widget.BaseTextView;
import com.dev.utils.app.ResourceUtils;
import com.dev.utils.app.ViewUtils;
import com.dev.utils.app.helper.QuickHelper;
import com.dev.utils.app.helper.ViewHelper;
import com.dev.utils.app.toast.ToastTintUtils;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

/**
 * detail: 多选辅助类 MultiSelectAssist
 *
 * @author Ttt
 */
public class MultiSelectActivity extends BaseActivity {

    private final static String TAG = "MultiSelectActivity";

    private ListContainer vidBvrList;

    private DirectionalLayout bar_layout;

    private DirectionalLayout bar;

    private Image select_retuen;

    private Text bar_name;

    // 适配器
    MultiSelectAdapter multiSelectAdapter;

    // 编辑按钮
    BaseTextView editView;
    // 取消编辑按钮
    BaseTextView cancelView;
    // 确定按钮
    BaseTextView confirmView;
    // 全选按钮
    BaseTextView allSelectView;
    // 非全选按钮
    BaseTextView unAllSelectView;
    // 反选按钮
    BaseTextView inverseSelectView;

    private List<CommodityEvaluateBean> lists = new ArrayList<>();

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_base_view_listContainer_select;
    }

    @Override
    protected void initView() {
        LogUtils.error(TAG, "initView()");
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_select_bar_layout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_multiselectassist_bar, null, false);
        bar_name = (Text) bar.findComponentById(ResourceTable.Id_bar_name);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_select_return);
        bar_layout.addComponent(bar);

        vidBvrList = (ListContainer) findComponentById(ResourceTable.Id_select_bvr_list);

        ComponentContainer parent = vidBvrList;
        // 根布局处理
        ViewHelper.get().setPadding(parent, 0)
                .setBackgroundColor(parent, ResourceUtils.getColorForInt(ResourceTable.Color_color_33));

        List<CommodityEvaluateBean> testlist = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            testlist.add(CommodityEvaluateBean.newCommodityEvaluateBean());
        }

        lists = testlist;

        // 初始化布局管理器、适配器
        multiSelectAdapter = new MultiSelectAdapter(lists, DevUtils.getContext())
                .setSelectListener(new MultiSelectAdapter.OnSelectListener() {
                    @Override
                    public void onClickSelect(
                            int pos,
                            boolean now
                    ) {
                        CommodityEvaluateBean commodityEvaluateBean = multiSelectAdapter.getData().get(pos);
                        LogUtils.error(TAG, "新状态: %s, 商品名: %s" + now + commodityEvaluateBean.commodityName);
                    }
                });
        vidBvrList.setItemProvider(multiSelectAdapter);
        addToolbarButton();

    }

    private void addToolbarButton() {
        bar.addComponent(editView = createTextView("编辑", new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                multiSelectAdapter.setEditState(true);
                // 显示其他按钮、隐藏编辑按钮
                ViewUtils.toggleVisibilitys(new Component[]{cancelView, confirmView, allSelectView, unAllSelectView, inverseSelectView}, editView);
                bar_name.setText("");
            }
        }));
        bar.addComponent(cancelView = createTextView("取消", new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                multiSelectAdapter.setEditState(false).clearSelectAll();
                // 显示编辑按钮、隐藏其他按钮
                ViewUtils.toggleVisibilitys(editView, cancelView, confirmView, allSelectView, unAllSelectView, inverseSelectView);
                bar_name.setText("多选辅助类 MultiSelectAs...");
            }
        }));
        bar.addComponent(confirmView = createTextView("确定", new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                StringBuilder builder = new StringBuilder();
                builder.append("是否全选: ").append(multiSelectAdapter.isSelectAll());
                builder.append("\n是否选中: ").append(multiSelectAdapter.isSelect());
                builder.append("\n选中数量: ").append(multiSelectAdapter.getSelectSize());
                builder.append("\n总数: ").append(multiSelectAdapter.getDataCount());
                ToastTintUtils.normal(builder.toString());
                LogUtils.error("TAG", builder.toString());
            }
        }));
        bar.addComponent(allSelectView = createTextView("全选", component -> multiSelectAdapter.selectAll()));
        bar.addComponent(unAllSelectView = createTextView("非全选", component -> multiSelectAdapter.clearSelectAll()));
        bar.addComponent(inverseSelectView = createTextView("反选", component -> multiSelectAdapter.inverseSelect()));

        // 显示编辑按钮
        ViewUtils.setVisibility(true, editView);
    }

    /**
     * 创建 TextView
     *
     * @param text            文案
     * @param onClickListener 点击事件
     * @return {@link BaseTextView}
     */
    private BaseTextView createTextView(
            String text,
            Component.ClickedListener onClickListener
    ) {
        return QuickHelper.get(new BaseTextView(this))
                .setVisibility(false) // 默认隐藏
                .setText(text)
                .setBold()
                .setTextColor(ResourceUtils.getColorForInt(ResourceTable.Color_white))
                .setTextSize(50)
                .setMarginTop(50)
                .setMarginLeft(50)
                .setOnClicks(onClickListener)
                .getView();
    }

    @Override
    protected void initListener() {
        select_retuen.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtils.error(TAG, "return 点击");
                onBackPressed();
            }
        });
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(bar);
        super.onBackground();
    }
}
